﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.PrintManager.Core;
using System.Data.SqlClient;

namespace POSPlus.PrintManager
{
	public class PrintJob : DatabaseView, IPrintJob
	{
		private String _jobName;
		private int _items;
		private String _type;
		private String _batchNo;

		public PrintJob(String connectionString, String jobName, int items, String type) 
			: base(connectionString)
		{
			_jobName = jobName;
			_items = items;
			_type = type;
		}

		public PrintJob(string connectionString, String jobName, int items, string type, string batchNo)
			: base(connectionString)
		{
			_jobName = jobName;
			_items = items;
			_type = type;
			_batchNo = batchNo;
		}

		public PrintJob(String connectionString, String jobName)
			: base(connectionString)
		{
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_PrintJobs WHERE JobName=@JobName",
					connection))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							_jobName = FromDB<String>(reader["JobName"]);
							_items = FromDB<int>(reader["Items"]);
							_type = FromDB<String>(reader["Type"]);
							_batchNo = string.IsNullOrEmpty(FromDB<string>(reader["BatchNo"])) ? "" : FromDB<string>(reader["BatchNo"]);
						}
						else
						{
							throw new DatabaseViewNotInstantiatedException("Unable to instantiate view with " +
								"JobName equal to " + jobName);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
		}

		public string JobName
		{
			get { return _jobName; }
		}

		public int Items
		{
			get { return _items; }
			set { _items = value; }
		}

		public string Type
		{
			get { return _type; }
		}

		public string BatchNo
		{
			get { return _batchNo; }
		}

		public static List<PrintJob> GetAll(String connectionString)
		{
			List<PrintJob> jobs = new List<PrintJob>();
			using(SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_PrintJobs", connection))
				{
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							PrintJob job = new PrintJob(connectionString, FromDB<String>(reader["JobName"]),
								FromDB<int>(reader["Items"]), FromDB<String>(reader["Type"]), FromDB<String>(reader["BatchNo"]));
							jobs.Add(job);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return jobs;
		}

		internal static List<PrintJob> GetAll(string connectionString, string jobName)
		{
			List<PrintJob> jobs = new List<PrintJob>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_PrintJobs WHERE JobName = @JobName", connection))
				{
					command.Parameters.AddWithValue("@JobName", jobName);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							PrintJob job = new PrintJob(connectionString, FromDB<string>(reader["JobName"]),
								FromDB<int>(reader["Items"]), FromDB<string>(reader["Type"]), FromDB<String>(reader["BatchNo"]));
							jobs.Add(job);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return jobs;
		}
	}
}